package com.wili.c.asm;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.Set;
import com.c.Ca;
import com.wili.c.Class9;
import net.bytebuddy.jar.asm.ClassReader;
import net.bytebuddy.jar.asm.Opcodes;
//import jdk.internal.org.objectweb.asm.ClassReader;
//import jdk.internal.org.objectweb.asm.Opcodes;
//import jdk.internal.org.objectweb.asm.tree.AbstractInsnNode;
//import jdk.internal.org.objectweb.asm.tree.ClassNode;
//import jdk.internal.org.objectweb.asm.tree.LineNumberNode;
//import jdk.internal.org.objectweb.asm.tree.MethodNode;
/**
 * 
 * @author weila 2021年2月18日
 */
@SuppressWarnings("rawtypes")
public class Asm9 {
	static int UseLog = -1, FunVisitor_useLog = -1;

	static final int AsmVerion = Opcodes.ASM5;

	static final String INIT = "<init>", CLINIT = "<clinit>";//初始化、静态初始化
	static Class<?> asmType(net.bytebuddy.jar.asm.Type type) {
		return Class9.classOfName(type.getClassName(), -1);
	}

	static Class<?>[] classesOfAsmTypes(net.bytebuddy.jar.asm.Type[] types) {
		Class<?>[] ret = new Class<?>[types.length];
		for (int i = 0; i < types.length; i++) {
			ret[i] = Class9.classOfName(types[i].getClassName(), -1);
		}
		return ret;
	}

	static Executable methodOf(Class<?> clazz, String funName, Class<?>[] paramTypes) {
		try {
			if (funName.equals(INIT) || funName.equals(clazz.getSimpleName())) {
				Constructor<?> cons = clazz.getDeclaredConstructor(paramTypes);
				return cons;
			} else {
				if (funName.equals(CLINIT))
					return null;
				if (++inc == 128)
					Ca.pause();
				Method md = Class9.findMethodUpwards(1, clazz, funName, paramTypes);
				return md;
			}
		} catch (Throwable e) {
			return null;
		}
	}

	static ClassReader newClassReader(String name) {
		try {
			return new ClassReader(name);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 	访问对属性的赋值地点：find every $findCls.findField=abcd in $inClass.
	 */
	public static void findPutField(File file, Class<?> inClass, Class<?> findCls, Set<String> findFields,
			FieldItems retFieldItems) {
		FindPutField9.findPutField(file, inClass, findCls, findFields, retFieldItems);
	}
	static int inc;
}